package com.carbon.carbonmanager.service;

import com.carbon.carbonmanager.mapper.DataAnalysisMapper;
import com.carbon.carbonmanager.model.AnalysisDataInfo;
import com.carbon.carbonmanager.unit.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class DataAnalysisService {
    @Autowired
    DataAnalysisMapper dataAnalysisMapper;
    /**
     * 首页数据统计
     * 所有人都可以看到大盘实时动态
     * */
    public Response getDataAnalysisy(){
        HashMap<String,Object> hs = new HashMap<>();
        //统计用户开户数量
        hs.put("enterCounts",getUserEnterCounts());
        //统计当日交易总量
        hs.put("total_quantity",getTradeQuantityToday().getTotal_quantity());
        //统计当日交易总额
        hs.put("total_amount",getTradeQuantityToday().getTotal_amount());
        //大盘近11天的每天交易进出量
        if(get11DaysTradeQuantityData()!=null){
            hs.put("days11Datas",get11DaysTradeQuantityData());
        }
        //统计所属行业企业数量
        hs.put("enterpriseData",getEnterpriseCounts());
        return new Response(200,"获取统计数据",hs);
    }

    /**
     * 统计用户开户数量
     * */
    private Integer getUserEnterCounts(){
        return dataAnalysisMapper.getUserEnterCounts();
    }

    /**
     * 统计今日交易量和今日交易额
     * */
    private AnalysisDataInfo getTradeQuantityToday(){
        AnalysisDataInfo analysisDataInfo = dataAnalysisMapper.getTradeQuantityToday();
        if(analysisDataInfo==null){
            AnalysisDataInfo asdi = new AnalysisDataInfo();
            asdi.setBuy_quantity(0);
            asdi.setSell_quantity(0);
            return asdi;
        }
        return analysisDataInfo;
    }

    /**
     * 统计近11日交易进出量
     * */
    private List<AnalysisDataInfo> get11DaysTradeQuantityData(){
        List<AnalysisDataInfo> asdi = dataAnalysisMapper.get11DaysTradeQuantityData();
        if(asdi==null){
            System.out.println("null");
            return null;
        }else{
            return asdi;
        }
    }

    /**
     * 企业所属行业数量
     * 统计各个行业包含的开户企业数量
     * */
    public List<AnalysisDataInfo> getEnterpriseCounts(){
        return dataAnalysisMapper.getEnterpriseCounts();
    }
}
